﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace EMS20.UI.Login
{
    using EMS20.Help;
    using EMS20.Common;
    using EMS20.Entity;
    using EMS20.BusinessLogicLayer;
    /// <summary>
    /// Login 的摘要说明
    /// </summary>
    public class Login : AjaxHelp
    {


        public override void SubProcessRequest()
        {
            string txtUserName = Request.Form["txtUseName"];
            string txtPwd = Request.Form["txtPwd"];
            string vcode = Request.Form["vcode"];
            string isRemember = Request.Form["isRemember"];

            if (string.IsNullOrEmpty(vcode))
            {
                base.WriteFail("验证码不能为空！");
                return;
            }
            if (Session[Kits.Vcode] == null)
            {
                base.WriteFail("验证码过期！");
                return;
            }
            string sessionVcode = Session[Kits.Vcode].ToString();

            //判断验证码是否正确
            if (!vcode.Equals(sessionVcode, StringComparison.OrdinalIgnoreCase))
            {
                base.WriteFail("验证码错误！");
                return;
            }

            //验证登录名和密码
            if (string.IsNullOrEmpty(txtPwd) || string.IsNullOrEmpty(txtUserName))
            {
                base.WriteFail("用户名和密码不正确");
                return;
            }

            string md5Pwd = Kits.GetMD5(txtPwd);



            UserInfoEntity entity = UserInfo_BLLSub.GetEntityByPasswordAndName(txtUserName, md5Pwd);

            if (entity == null)
            {
                base.WriteFail("用户名或密码错误");
                return;
            }

            Session[Kits.Entity] = entity;

            if (!string.IsNullOrEmpty(isRemember))
            {
                HttpCookie cookie = new HttpCookie(Kits.CookieId, entity.user_id.ToString());

                cookie.Expires = DateTime.Now.AddDays(7);

                Response.Cookies.Add(cookie);


            }
            base.WriteSuccess("登陆成功");



        }
    }
}